package com.tj.ocr.img;

import java.awt.image.BufferedImage;

@SuppressWarnings("unused")
public class TextPop {

	private static final int full_width = 14;
	public static int start_pos = 0;
    private static int is = 0;
	
	private MyImage srcImg;
	private int pos = 0;
	private int pos_y = 0;
	private int width = 0;
	private int height = 0;

	public TextPop(MyImage img) {
		this.srcImg = img;
		this.width = img.getWidth();
		this.height = img.getHeight();
		
		this.pos = start_pos;
		this.pos_y = img.getFromy();
	}
	
	public MyImage pop() {
		try {
			int from = pos,  tof = from + full_width;
			if (tof >= width) return null;

			MyImage thimg = subImage(srcImg, from, pos_y, tof - from, height);
			is++;
			//ImageRW.write(thimg.realImage(), "d:\\test\\c1\\ "+is+".png");
			//ImageRW.write(thimg, "d:\\test\\c1\\ "+is+".jpg");
			
			pos += full_width;
			
			//rst = ocr.doOCR(thimg);
			//System.out.println("2nd reg: " + name);
			
			return thimg;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	public static MyImage subImage(MyImage image, int fromx, int fromy, int width, int height) {
		int ry = fromy, rh = height;
		int[][] data = image.getData();
		
		//header check
		for(int i=fromy;i<fromy + height/2;i++){
			boolean hasData = false;
			for(int j=fromx;j<fromx + width;j++){
				//System.out.print(j+","+i + " ");
				if(data[j][i] == 1){//Project.hasData(image,j,i)){
					rh = rh - i + fromy;
					ry = i;
					hasData = true;
					break;
				}
			}
			if(hasData) break;
		}
		
		//from root
		for(int i=fromy+height-1;i>fromy + height/2;i--){
			boolean hasData = false;
			for(int j=fromx;j<fromx + width;j++){
				if(data[j][i] == 1){
					rh = rh - (fromy+height-1-i);
					hasData = true;
					break;
				}
			}
			if(hasData) break;
		}
				
		return new MyImage(image.getImage(),data, fromx, ry, width, rh);// image.getSubimage(fromx, ry, width, rh);
	}
	
	public static BufferedImage subImage0(BufferedImage image, int fromx, int fromy, int width, int height) {
		int ry = fromy, rh = height;
		//header check
		/*for(int i=fromy;i<fromy + height/2;i++){
			boolean hasData = false;
			for(int j=fromx;j<fromx + width;j++){
				//System.out.print(j+","+i + " ");
				if(Project.hasData(image,j,i)){
					rh = rh - i;
					ry = i;
					hasData = true;
					break;
				}
			}
			if(hasData) break;
		}
		//from root
		for(int i=fromy+height-1;i>fromy + height/2;i--){
			boolean hasData = false;
			for(int j=fromx;j<fromx + width;j++){
				if(Project.hasData(image,j,i)){
					rh = rh - (fromy+height-1-i);
					hasData = true;
					break;
				}
			}
			if(hasData) break;
		}*/
		
		//System.out.print(rh + ":");
		
		return image.getSubimage(fromx, ry, width, rh);
	}

	public static void main(String[] args) {
		//TextPop.init();
		
		TextPop pop = new TextPop(BlankWhiteUtil.procEx(ImageRW.read("d:\\test\\tt.png"),170));
		while(pop.pop() != null){
			System.out.println(1);
		}
	}
}
